<template>
    <div>
       <Row type="flex" justify="space-between" align="middle">
            <Col span="15" class="title">栏目列表</Col>
            <Col span="9" :style="{textAlign: 'right'}">
                <Button icon="md-add" @click="addCat">添加栏目</Button>
                <Button type="default" @click="clickOrderBtn">排序</Button>
            </Col>
       </Row>
       <div class="table-box">
           <Table width="100%" :height="height" border :columns="columns" :data="data" :loading="loading"></Table>
       </div>
    </div>
</template>
<script>
    export default {
        data () {
            var height = this.$util.getClientHeight()-64-51-30-58-15;

            // 菜单类型
            var catType = this.$fieldType.catType;
            var that = this;

            return {
                loading: true,
                height: height,
                indexObj: {},
                columns: [
                    {
                        title: '排序',
                        key: 'orderlist',
                        width: 80,
                        render: (h, params) => {
                            var flag = params.row.flag ? 0 : 1;
                            var input = [
                                h('Input', {
                                    props: {
                                        size: 'small',
                                        value: params.row.listorder
                                    },
                                    on: {
                                        input: (val)=>{
                                            var item = {};
                                            item.listorder = parseInt(val);
                                            item.id = params.row.id;
                                            that.indexArrAdd(item);
                                        }
                                    }
                                }, '')];
                            return h('div', input);
                        }
                    },
                    {
                        title: 'ID',
                        key: 'id',
                        width: 70
                    },
                    {
                        title: '名称',
                        key: 'Xname',
                        width: 150
                    },
                    {
                        title: '模型类型',
                        key: 'modelName',
                        width: 150
                    },
                    {
                        title: '栏目类型',
                        key: 'type',
                        width: 150,
                        render: (h, params) => {
                            var type = params.row.type;
                            return h('span', catType[type]);
                        }
                    },
                    {
                        title: '创建者',
                        key: 'createName',
                        width: 100
                    },
                    {
                        title: '创建时间',
                        key: 'createTime',
                        width: 170
                    },
                    {
                        title: '操作',
                        key: 'action',
                        width: 250,
                        render: (h, params) => {
                            var buttonAction = [
                                h('Button', {
                                    props: {
                                        type: 'primary',
                                        size: 'small'
                                    },
                                    style: {
                                        marginRight: '5px'
                                    },
                                    on: {
                                        click: ()=>{
                                            that.editCat(params);
                                        }
                                    }
                                }, '编辑'),
                                h('Button', {
                                    props: {
                                        type: 'error',
                                        size: 'small'
                                    },
                                    style: {
                                        marginRight: '5px'
                                    },
                                    on: {
                                        click: ()=>{
                                            that.delCat(params.row);
                                        }
                                    }
                                }, '删除')
                                ];

                            if(params.row.type === 0){
                                buttonAction.push(h('Button', {
                                                    props: {
                                                        type: 'default',
                                                        size: 'small'
                                                    },
                                                    on: {
                                                        click: ()=>{
                                                                this.$router.push({
                                                                            path: '/admin/addCat',
                                                                            query: params.row
                                                                        });
                                                        }
                                                    }
                                                }, '添加子菜单'));
                            }

                            return h('div', buttonAction);
                        }
                    }
                ],
                data: []
            }
        },
        created: function () {
            // 获取栏目列表信息
            this.getCatList();
        },
        methods: {
            // 获取栏目所有信息列表
            getCatList(){
                var that = this;
                that.$util.ajax({
                    that: that,
                    url: '/category/catlist/',
                    success: function(data){
                        that.data = that.handleCatList(data.data);
                        that.loading = false;
                    },
                    fail: function(msg){
                        that.$Message.info(msg);
                    }
                })
            },
            // 处理返回的栏目列表
            handleCatList(data){
                var catData = [];

                // 提取顶级菜单
                catData = foreachData(data);

                // 处理数据
                function foreachData(data){
                    var dataList = [];
                    var i=0

                    for(; i<data.length; i++){
                        var item = data[i];
                        if(item.parentId === 0){
                            item.Xname = item.name;
                            dataList.push(item);
                            data.splice(i, 1);
                            i--;
                            dataList = dataList.concat(travData(data, item.id));
                        }
                    }

                    // 遍历杂乱tree结构数组处理成并列挨着排列的数组
                    function travData(iData, id, z=1) {
                        var dataList = [];
                        for(var j=0; j<iData.length; j++){
                            var item = iData[j];
                            var space = '';
                            if(item.parentId === id){
                                for(var m=0; m<z; m++){
                                    space = space + '---';
                                }

                                item.Xname = space + item.name;
                                dataList.push(item);
                                iData.splice(j, 1);
                                j--;
                                if(j<i){
                                    i--;
                                }
                                dataList = dataList.concat(travData(iData, item.id, z+1));
                            }
                        }
                        return dataList;
                    }
                    return dataList;
                }

                return catData;
            },
            // 删除栏目
            delCat (data){
                var that = this;
                that.$loading.show('更新中');
                that.$util.ajax({
                    that: that,
                    url: '/category/del/',
                    data: {
                        id: data.id
                    },
                    success: function(data){
                        that.$Message.info('删除成功！');
                        that.getCatList();
                    },
                    fail: function(msg){
                        that.$Message.info(msg);
                    },
                    complete: function(){
                        that.$loading.hide();
                    }
                })
            },
            // 编辑栏目
            editCat(params){
                this.$router.push({
                                    path: '/admin/editCat',
                                    query: params.row
                                });
            },
            // 添加菜单
            addCat(){
                this.$router.push({
                                    path: '/admin/addCat',
                                    query: {
                                        id: 0
                                    }
                                });
            },
            // 添加修改排序
            indexArrAdd(item){
                var indexObj = this.indexObj
                if(indexObj[item.id]){
                    indexObj[item.id]['listorder'] = isNaN(item.listorder)?0:item.listorder;
                } else {
                    indexObj[item.id] = item;
                }
            },
            // 点击排序按钮
            clickOrderBtn(){
                var indexObj = this.indexObj;
                var indexArr = [];
                var that = this;

                Object.keys(indexObj).forEach(function(key){
                    indexArr.push(indexObj[key]);
                });

                if(indexArr.length === 0){
                    that.$Message.info('请先修改排序，在提交排序！');
                    return;
                }

                that.$loading.show('更新中');
                that.$util.ajax({
                    that: that,
                    url: '/category/order/',
                    data: indexArr,
                    success: function(data){
                        that.$Message.info(data.msg);
                        that.indexObj = {};
                        that.getCatList();
                    },
                    fail: function(msg){
                        that.$Message.info(msg);
                    },
                    complete: function(){
                        that.$loading.hide();
                    }
                })
            }
        }
    }
</script>
<style scoped>
</style>